/*
 * Copyright (c) 2008, Artur Emagulov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

/**
 * @author Artur Emagulov
 * 
 * Kiddie-OS Java UART driver example
 * 
 */
public class JvmTest {


	int Qwe1 = 12431;
	short Qwe2s = 23423;

	public static final int HELLO_WORLD_FIELD = 123456789;
	
	private byte[] Hello = {'H','e','l','l','o',' ','W','o','r','l','d','!','\n'};
	private byte[] data;
	private short[] data2;
	
	/**
	 * Constructor
	 */
	public JvmTest() {
		int a = 98765;
		int b = 12345;
		byte q;
		data = _palloc(0x3f8, 0x1000); /* map uart_base */
		data2 = ba2sa(data);
		putchar((byte)'1');
		initialize();

		a = -a;
		b += a;
		b--;
		
		putchar((byte)b);
		
		for (int i = 0; i<Hello.length; i++)
		{
			q = putchar((byte)Hello[i]);
		}
		
	}

	/**
	 * This initializes UART
	 */
	int initialize() {
		return 0;
	}

	/**
	 * 
	 */
	public static byte[] _palloc(int addr, int size) {
		return palloc(addr, size);
	}

	/**
	 * 
	 */
	private byte putchar(byte b)
	{
		outw(b,ba2vaddr(data));
		// data2[0] = b;
		return b;
	}

	/** maps physical memory from 'addr' to virtual memory space */
	private static native byte[] palloc(int addr, int size);
	/** converts byte array to short array */
	private static native short[] ba2sa(byte[] ba);
	/** converts byte array to int array */
	private static native int[] ba2ia(byte[] ba);
	/** takes physical address of byte array */
	private static native int ba2paddr(byte[] ba);
	/** takes virtual address of byte array */
	private static native int ba2vaddr(byte[] ba);

	/** port IO  */
	private static native void outl(int v, int p);
	private static native void outw(int v, int p);
	private static native void outb(int v, int p);
	private static native int inl(int p);
	private static native int inw(int p);
	private static native int inb(int p);
	
}
